﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Phone.UserData;
using SMSHelper;
using SMSHelper.Helper;
using System.Diagnostics;

namespace SMSHelper.Data
{
    public class DataProvider
    {
        #region data source
        const string connectString = "Data Source=isostore:/phoneNumber.sdf";
        #endregion

        #region public method
        /// <summary>
        /// get numbers from contacts , insert to database
        /// </summary>
        /// <param name="contacts"></param>
        public static void InitNumbers(List<PhoneNumberInfo> contacts)
        {
            if (contacts == null) return;
            if (contacts.FirstOrDefault() == null) return;

            try
            {
                using (NumberDataContext dataContext = new NumberDataContext(connectString))
                {
                    if (dataContext.DatabaseExists())
                    {
                        dataContext.DeleteDatabase();
                    }

                    dataContext.CreateDatabase();
                    dataContext.PhoneNumbers.InsertAllOnSubmit(contacts);
                    dataContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        /// <summary>
        /// delete all data from database
        /// </summary>
        public static void DeleteAllNumbers()
        {
            try
            {
                using (NumberDataContext dataContext = new NumberDataContext(connectString))
                {
                    if (dataContext.DatabaseExists())
                    {
                        dataContext.DeleteDatabase();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        /// <summary>
        /// get all numbers from database
        /// </summary>
        /// <returns></returns>
        public static List<PhoneNumberInfo> GetAllNumber()
        {
            try
            {
                using (NumberDataContext dataContext = new NumberDataContext(connectString))
                {
                    if (!dataContext.DatabaseExists())
                    {
                        return null;
                    }

                    var numbers = from PhoneNumberInfo phoneNumber in dataContext.PhoneNumbers select phoneNumber;
                    return numbers.ToList();
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                Debug.WriteLine(ex);
                return null;
            }
        }
        #endregion
    }
}
